package com.hhh.demo.sx.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhh.demo.sx.bean.*;
import com.hhh.demo.sx.service.MenuService;

import com.hhh.demo.sx.service.RoleMenuService;
import com.hhh.demo.sx.util.ResultMap;
import com.hhh.demo.sx.util.ResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hhh
 * @since 2021-12-27
 */
@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;
    /**
     * 打开菜单主页面
     */
    @RequestMapping("/index")
    public String openPage(){
        return "menu";
    }

    /**
     * 查询菜单列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map<String, Object> getList(@RequestParam("page") int p,
                                       @RequestParam("limit") int limit,
                                       @RequestParam(value = "condition", required = false) String condition){
        IPage<Menu> Page = null;
        QueryWrapper<Menu> warpper = new QueryWrapper<>();
        Page<Menu> pages = new Page<>(p, limit);;
        JSONObject jsonObject = JSONObject.parseObject(condition);
        if (jsonObject != null) {
            String username = (String) jsonObject.get("name");
            warpper.like(!username.equals(""), "name", username);
            Page = menuService.findAll(pages, warpper);
        } else {
            Page = menuService.findAll(pages, null);
        }

        return  ResultPage.Page(Page.getRecords(),Page.getTotal());

    }
    /**
     * 新增/修改菜单
     */
    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    public ResultMap saveOrUpdate(Menu menu){
        if(menu.getParentId()!=0){
            Menu m=menuService.getById(menu.getParentId());
            menu.setParentName(m.getName());
        }
        boolean f=menuService.saveOrUpdate(menu);
        if(f){
            return ResultMap.success();
        }else {
            return ResultMap.fail();
        }
    }
    /**
     * 查找菜单
     */
    @ResponseBody
    @RequestMapping("/getOne")
    public ResultMap getOne(int id){
        Menu menu= menuService.getById(id);
        return ResultMap.success(menu);
    }
    /**
     * 删除菜单
     *  @Transactional 事务
     */
    @Transactional
    @ResponseBody
    @RequestMapping("/delete")
    public ResultMap delete(int id){
        boolean f=menuService.removeById(id);
        QueryWrapper<RoleMenu> wrapper =new QueryWrapper<>();
        wrapper.eq("menu_id",id);
        roleMenuService.remove(wrapper);
        if(f){
            return ResultMap.success();
        }else {
            return ResultMap.fail();
        }
    }
    /**
     * 获取所有菜单
     */
    @ResponseBody
    @RequestMapping("/getALL")
    public ResultMap delete(){
       return ResultMap.success(menuService.list());
    }

    /**
     * 获取菜单的树结构
     */
    @ResponseBody
    @RequestMapping("/getTree")
    public ResultMap getTree(Integer id){
//        Map<Menu,List> map=new HashMap<>();
//        List<LayuiTree> trees=new ArrayList<>();
//        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
//        wrapper.eq("parent_id",0);
//        List<Menu> menu=menuService.list(wrapper);
//        wrapper.clear();
//        for (Menu m:menu){
//            /**
//             * 本菜单
//             */
//            LayuiTree tree=new LayuiTree();
//            tree.setId(m.getId());
//            tree.setParent_id(m.getParentId());
//            tree.setTitle(m.getName());
//            /**
//             * 每个菜单的子菜单
//             */
//            List<LayuiTree> children=new ArrayList<>();
//            wrapper.eq("parent_id",m.getId());
//            List<Menu> mc=menuService.list(wrapper);
//            wrapper.clear();
//            for (Menu mm:mc){
//                LayuiTree tree1=new LayuiTree();
//                tree1.setId(mm.getId());
//                tree1.setParent_id(mm.getParentId());
//                tree1.setTitle(mm.getName());
//                children.add(tree1);
//            }
//            tree.setChildren(children);
//            trees.add(tree);
//
//         }
        List<LayuiTree> tree=menuService.selectMenuTree(id);
//        System.out.println(tree.get(1).getChecked());
        return ResultMap.success(tree);
    }


    @ResponseBody
    @RequestMapping("/login")
    public ResultMap menu(HttpSession session){
        User user= (User) session.getAttribute("user");
        return  ResultMap.success(menuService.selectByUserId(user.getId()));
    }


}

